package king.dominic.jlibrary.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;

import king.dominic.jlibrary.util.FragmentFactory;
import king.dominic.jlibrary.view.WindowUtil;

/**
 * Created by king on 2017/3/31.
 *
 * @see OriginFragmentActivity
 */

public class OriginActivity extends OriginPermissionActivity implements FragmentFactory.FragmentParent {
    private final String TAG = getClass().getSimpleName();
    protected WindowUtil.WindowAttribute windowAttribute;
    private HashMap<String, Object> mFragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentFactory.parseSavedInstanceState(this, savedInstanceState);
        WindowUtil.initWindow(this);
        initWindowAttribute();
    }

    @Override
    public void setTitle(CharSequence title) {
        initWindowAttribute();
        WindowUtil.setTitle(windowAttribute, title);
    }

    @Override
    public void setTitle(int titleId) {
        initWindowAttribute();
        WindowUtil.setTitle(windowAttribute, titleId);
    }

    public void showToast(final int msg) {
        showToast(getString(msg));
    }
    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        windowAttribute.contentLayoutResourceId = layoutResID;
        View content = WindowUtil.setContentView(windowAttribute);
        windowAttribute.initContentView = true;
        super.setContentView(content);
    }

    @Override
    public void setContentView(View view) {
        windowAttribute.contentView = view;
        View content = WindowUtil.setContentView(windowAttribute);
        windowAttribute.initContentView = true;
        super.setContentView(content);
    }

    public void showBlackStatusBar() {
        WindowUtil.setStatusBarContentDark(this);
    }

    public void addLeftAction(WindowUtil.TitleBarAttribute.Action action) {
        initWindowAttribute();
        WindowUtil.addLeftAction(windowAttribute, action);
    }

    public void addRightAction(WindowUtil.TitleBarAttribute.Action action) {
        initWindowAttribute();
        WindowUtil.addRightAction(windowAttribute, action);
    }

    public void removeLeftAction(WindowUtil.TitleBarAttribute.Action action) {
        initWindowAttribute();
        WindowUtil.removeLeftAction(windowAttribute, action);
    }

    public void removeRightAction(WindowUtil.TitleBarAttribute.Action action) {
        initWindowAttribute();
        WindowUtil.removeRightAction(windowAttribute, action);
    }

    public void setTitleBarText(CharSequence text) {
        initWindowAttribute();
        WindowUtil.setTitle(windowAttribute, text);
    }

    private void initWindowAttribute() {
        if (windowAttribute == null) {
            synchronized (this) {
                if (windowAttribute == null)
                    windowAttribute = new WindowUtil.WindowAttribute(this);
            }
        }
    }

    public void removeAllLeftActions() {
        initWindowAttribute();
        WindowUtil.removeAllLeftAction(windowAttribute);
    }

    public void removeAllRightActions() {
        initWindowAttribute();
        WindowUtil.removeAllRightAction(windowAttribute);
    }

    public void notifyRightAction(WindowUtil.TitleBarAttribute.Action action) {
        initWindowAttribute();
        WindowUtil.notifyRightAction(windowAttribute, action);
    }

    public WindowUtil.WindowAttribute getWindowAttribute() {
        initWindowAttribute();
        return windowAttribute;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (outState == null) outState = new Bundle();
        FragmentFactory.onSaveInstanceState(this, outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public HashMap<String, Object> getFragments() {
        return mFragments;
    }

    @Override
    public void setFragments(HashMap<String, Object> fragments) {
        this.mFragments = fragments;
    }

    @Override
    public FragmentManager gFragmentManager() {
        return getSupportFragmentManager();
    }
}
